﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Design;
using SedionFormDesigner.CusControls.Infrastructure;
using SedionFormDesigner.CusControls.TypeEditor;
using SedionFormDesigner.CusControls.TextBox;

namespace SedionFormDesigner.CusControls
{
    public class BD_TextBox : System.Windows.Forms.TextBox
    {
        #region 属性:外观

        private float _fontsize = 9;//字体大小
        private bool _fontbold = false;//是否粗体
        private bool _fontitalic = false;//是否斜体
        private TextAlign _textAlign = TextAlign.left;
        private String _name = String.Empty;

        [Category("设计")]
        [ReadOnly(true)]
        public String NewName
        {
            get
            {
                _name = _name == String.Empty ? base.Name : _name;
                return _name;
            }
            set
            {
                _name = value;
                base.Name = _name;
            }
        }

        [Category("外观")]
        public float FontSize
        {
            set { _fontsize = value; FontChange(); }
            get { return _fontsize; }
        }

        [Category("外观")]
        public bool FontBold
        {
            set { _fontbold = value; FontChange(); }
            get { return _fontbold; }
        }

        [Category("外观")]
        public bool FontItalic
        {
            set { _fontitalic = value; FontChange(); }
            get { return _fontitalic; }
        }

        [Category("外观")]
        public new TextAlign TextAlign
        {
            set
            {
                _textAlign = value;
                switch (_textAlign)
                {
                    case TextAlign.left:
                        base.TextAlign = HorizontalAlignment.Left;
                        break;
                    case TextAlign.center:
                        base.TextAlign = HorizontalAlignment.Center;
                        break;
                    case TextAlign.right:
                        base.TextAlign = HorizontalAlignment.Right;
                        break;
                    default:
                        break;
                }
            }
            get { return _textAlign; }
        }

        void FontChange()
        {
            if (_fontbold && _fontitalic)
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize, FontStyle.Bold | FontStyle.Italic);
            else if (_fontbold && !_fontitalic)
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize, FontStyle.Bold);
            else if (!_fontbold && _fontitalic)
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize, FontStyle.Italic);
            else
                base.Font = new Font(base.Font.FontFamily.Name, _fontsize);
        }

        #endregion

        #region 属性:限制

        private Limitype _limittype;
        private string _limit = string.Empty;

        [DisplayName("类型"), Category("限制")]
        public Limitype Limittype
        {
            set { _limittype = value; }
            get { return _limittype; }
        }

        /// <summary>
        ///  不知道什么用 待用
        /// </summary>
        [DisplayName("条件"), Category("限制")]
        [Description("不知道什么用")]
        public string Limit
        {
            set { _limit = value; }
            get { return _limit; }
        }

        private String formular;
        [Category("杂项")]
        [Description("设计计算公式")]
        [Browsable(true)]
        [Editor(typeof(FormularEditor), typeof(UITypeEditor)), Localizable(true)]
        public String Formular
        {
            get { return formular; }
            set { formular = value; }
        }

        #endregion

        #region 工作流参数
        //private bool _iswfArg;
        private string _argName;

        //[Category("工作流参数")]
        //public bool IswfArg
        //{
        //    set
        //    {
        //        _iswfArg = value;
        //        //SetPropertyVisibility(this, "ArgName", value);
        //    }
        //    get { return _iswfArg; }
        //}

        //private void SetPropertyVisibility(object obj, string propertyName, bool visible)
        //{
        //    try
        //    {
        //        Type type = typeof(BrowsableAttribute);
        //        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
        //        AttributeCollection attrs = props[propertyName].Attributes;
        //        FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
        //        fld.SetValue(attrs[type], visible);
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        [Category("工作流参数")]
        public string ArgName
        {
            set { _argName = value; }
            get { return _argName; }
        }

        #endregion

        #region 自动绑定参数

        private Autobing _autoBind;

        public Autobing AutoBind
        {
            set
            {
                switch (value)
                {
                    case Autobing.不绑定:
                        ControlHelper.SetPropertyVisibility(this, "Formular", true);
                        break;
                    default:
                        ControlHelper.SetPropertyVisibility(this, "Formular", false);
                        break;
                }
                this._autoBind = value;
            }
            get { return this._autoBind; }
        }

        #endregion
    }
}
